﻿using Common.Base;
using ImTools;
using Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SanmaoSystem.ViewModels
{
    public class LoginWindowViewModel : BindableBase
    {   
        //定义用户名
        private string _userName="admin";
        public string UserName { get { return _userName; } set { SetProperty(ref _userName, value); } }
        //定义密码
        private string _password="123456";
        public string Password { get { return _password;}set { SetProperty(ref _password, value);} }
        //定义登录命令
        public  DelegateCommand<object> LoginCommand { get =>  new DelegateCommand<object>(Login); }
        
        public  void Login(object parameter)
        {
            MemberIProvider memberIProvider= new MemberIProvider();
            MD5Utils mD5Utils= new MD5Utils();
            var list = memberIProvider.GetAll();
            var user= list.FirstOrDefault(item =>  item.Password == mD5Utils.ComputeHash(Password) && item.Name==UserName);
            if(user == null)
            {
                MessageBox.Show("用户或密码错误！");
            }
            else
            {    
                Application.Current.Properties["UserName"] = UserName;
                (parameter as Window).DialogResult = true;
            }
        }
    }
}
